import { create } from 'zustand';
import { McpService, McpTool } from '../components/ToolManager';

// 定义 MCP 状态类型
interface McpState {
  mcpServices: McpService[];
  mcpTools: McpTool[];

  // MCP setters
  setMcpServices: (services: McpService[]) => void;
  addMcpService: (service: McpService) => void;
  removeMcpService: (id: string) => void;
  updateMcpService: (id: string, updates: Partial<McpService>) => void;

  setMcpTools: (tools: McpTool[]) => void;
  addMcpTool: (tool: McpTool) => void;
  removeMcpTool: (name: string) => void;
}

// 创建 MCP store
export const useMcpStore = create<McpState>()((set) => ({
  mcpServices: [],
  mcpTools: [],

  // MCP setters
  setMcpServices: (services) => set({ mcpServices: services }),
  addMcpService: (service) => set((state) => ({
    mcpServices: [...state.mcpServices, service]
  })),
  removeMcpService: (id) => set((state) => ({
    mcpServices: state.mcpServices.filter(service => service.id !== id)
  })),
  updateMcpService: (id, updates) => set((state) => ({
    mcpServices: state.mcpServices.map(service =>
      service.id === id ? { ...service, ...updates } : service
    )
  })),

  setMcpTools: (tools) => set({ mcpTools: tools }),
  addMcpTool: (tool) => set((state) => ({
    mcpTools: [...state.mcpTools, tool]
  })),
  removeMcpTool: (name) => set((state) => ({
    mcpTools: state.mcpTools.filter(tool => tool.name !== name)
  })),
}));
